//index.js
const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    select: ['积分卡', '美睫卡', '美甲卡',"激光脱毛",'充值卡'],
    currentTab: 0,
    customerListData:[],
    customerList1:[],
    customerList2:[],
    customerList3:[],
    customerList4:[],
    customerList5:[],
    customerListLen:[],
    searchName:'',
    swiperRPXHeight: 0
  },

  onLoad: function(options) {
    let _that = this;
    wx.getSystemInfo({
      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let rpxHeight = clientHeight * ratio;
        _that.setData({
          swiperRPXHeight: rpxHeight - 230
        })
      },
    })
    //先拿缓存的数据，没有在拿数据库
    // let cdata = wx.getStorageSync('customerData')
    // if(cdata.length>0){
    //   this.setData({
    //     customerListData:cdata
    //   })
    //   this.getData()
    // }else{
    // }
    //请求数据库
    this.requestData()
    if(options.type){
      this.setData({
        currentTab: +options.type-1
      })
    }
  },
  requestData(){
    wx.cloud.callFunction({
      name:'updateInfo',
      success:res=>{
        let data = res.result
        this.setData({
          customerListData:data
        })
        wx.setStorageSync('customerData', data)
        this.getData()
      },
      fail:err=>{
      }
    })
  },
  getData(){
    let name = this.data.searchName
    //查询
    let filterData = this.data.customerListData
    console.log(filterData)
    if(name){
      let filterName = this.data.customerListData.filter(ele=>{
        return ele.name.indexOf(name)>-1 ? true : false
      })
      let filterTel = this.data.customerListData.filter(ele=>{
        return ele.tel.indexOf(name)>-1 ? true : false
      })
      filterData = [...filterName,...filterTel]
    }
    let arr1 = filterData.filter(ele=>ele.type==1)
    let arr2 = filterData.filter(ele=>ele.type==2)
    let arr3 = filterData.filter(ele=>ele.type==3)
    let arr4 = filterData.filter(ele=>ele.type==4)
    let arr5 = filterData.filter(ele=>ele.type==5)
    this.setData({
      customerList1:arr1,
      customerList2:arr2,
      customerList3:arr3,
      customerList4:arr4,
      customerList5:arr5,
      customerListLen:[arr1.length,arr2.length,arr3.length,arr4.length,arr5.length]
    })
    // let searchWork = db.RegExp({
    //   regexp: name,
    // })
    // wx.cloud.callFunction({
    //   name:'updateInfo',
    //   data:{type:type,name:searchWork,tel:searchWork},
    //   success:res=>{
    //     console.log(res)
    //     this.setData({
    //       customerList2:res.result.data
    //     })
    //   },
    //   fail:err=>{
    //   }
    // })
  },
  new(){
    wx.navigateTo({
      url: '/pages/new/new',
    })
  },
  edit(e){
    wx.navigateTo({
      url: '/pages/new/new?id='+e.currentTarget.dataset.id,
    })
  },
  selectList(event) {
    this.setData({
      currentTab: event.currentTarget.dataset.index
    })
  },
  changeSelect(event) {
    this.setData({
      currentTab: event.detail.current
    })
    this.getData()
  },
  bindinput(e){
    let searchName = e.detail.value
    this.setData({
      searchName
    })
    this.getData()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉事件触发")
    this.requestData()
    wx.stopPullDownRefresh()
  }
})
